package pers.supeijin.devtools.converter.wx2vue.converter;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.XmlRecursiveElementVisitor;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlTag;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;

import kotlin.Pair;
import pers.supeijin.devtools.converter.utils.FilePathUtils;
import pers.supeijin.devtools.utils.GeneralContext;

//资源转移器
public class ResourceTransfer {
	private static final String RESOURCE_PAITH = "/src/assets/images/transfer";
	private Project mProject;
	private VirtualFile mSlectedVirtualFile;
	private GeneralContext mGeneralContext;
	private String mSlectedVirtualFilePath;

	public ResourceTransfer(GeneralContext generalContext, VirtualFile slectedVirtualFile) {
		mGeneralContext = generalContext;
		mProject = generalContext.getProject();
		mSlectedVirtualFile = slectedVirtualFile;
		mSlectedVirtualFilePath=slectedVirtualFile.getPath();
	}

	//开始转换
	public void transfer(PsiFile psiFile) {
		//获取目标地址
		final String resourcePath = mProject.getBasePath() + RESOURCE_PAITH;
		File file = new File(resourcePath);
		if (!file.exists()) {
			file.mkdirs();//创建文件夹
		}
		String path = mGeneralContext.getVirtualFile().getPath();
		//String baseFilePath = path + File.separator + "temp.vue";
		File baseFile = new File(path);
		ArrayList<Pair<String, XmlAttribute>> list = getTransferList(psiFile);
		list.forEach(pairInfo -> {
			XmlAttribute xmlAttribute = pairInfo.getSecond();
			String attributeValue = xmlAttribute.getValue();
			String fileName = attributeValue.substring(attributeValue.lastIndexOf("/") + 1);
			String imageFilePath = file.getPath() + File.separator + fileName;//转移后的目标文件地址
			File newImageFile = new File(imageFilePath);
			String relativePath = FileUtil.getRelativePath(baseFile, newImageFile);
			relativePath=relativePath.replaceAll("\\\\","/");
			xmlAttribute.setValue(relativePath);
			File oldImageFile = new File(pairInfo.getFirst());
			try {
				if (!newImageFile.exists()) {
					FileUtil.copy(oldImageFile, newImageFile);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		});
	}

	//获取、准备转换的图片资源相关信息
	private ArrayList<Pair<String, XmlAttribute>>  getTransferList(PsiFile psiFile) {
		HashMap<String, Pair<String, XmlAttribute>> map = new HashMap<>();
		ArrayList<Pair<String, XmlAttribute>> arrayList = new ArrayList<>();
		psiFile.accept(new XmlRecursiveElementVisitor() {
			@Override
			public void visitXmlAttribute(XmlAttribute attribute) {
				super.visitXmlAttribute(attribute);
				if (Objects.equals(attribute.getName(), "src")) {
					XmlTag parent = attribute.getParent();
					if (Objects.equals(parent.getName(), "image") || Objects.equals(parent.getName(), "cover-image")) {
						String attributeValue = attribute.getValue();
						Pair<String, XmlAttribute> attributePair = map.get(attributeValue);
						if (attributePair == null) {
							String targetFilePath = FilePathUtils.getTargetFilePath(mSlectedVirtualFilePath, attributeValue);
							if (targetFilePath != null && targetFilePath.length() > 0) {
								attributePair= new Pair<>(targetFilePath, attribute);
								map.put(attributeValue,attributePair);
								arrayList.add(attributePair);
							}
						}else{
							arrayList.add(new Pair<>(attributePair.getFirst(),attribute));
						}
					}
				}
			}
		});
		return arrayList;
	}
}
